﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Game
{
    namespace Events
    {
        public class UserInput
        {
            public enum Key : uint
            {
                None = 0,
                A = 65, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
                //
                Esc = 27,
                Shift = 16,
                Ctrl = 17,
                Space = 32,
                //Arrow
                ArrowLeft = 37,
                ArrowUp = 38,
                ArrowRight = 39,
                ArrowDown = 40,
                //Mouse
                MouseLeft = 520,
                MouseMiddle = 521,
                MouseRight = 523,
                MouseWhellUp = 524,
                MouseWhellDown = 525,
            }

            private Key mCode;

            public UserInput(Key code)
            {
                mCode = code;
            }

            public bool Compare(UserInput cmp)
            {
                return cmp.Code == Code;
            }

            public Key Code
            {
                get { return mCode; }
            }
        }

        public class MouseUserInput : UserInput
        {
            private IDK.Vector2 mMouseMove;

            public MouseUserInput(IDK.Vector2 xy, Key code)
                : base(code)
            {
                mMouseMove = xy;
            }

            public IDK.Vector2 Move
            {
                get { return mMouseMove; }
            }
        }

        public interface IUserInputListener
        {
            void OnKeyDown(Events.UserInput input);
            void OnKeyUp(Events.UserInput input);
            void OnMouseMove(Events.MouseUserInput input);
            void OnMouseDown(Events.MouseUserInput input);
            void OnMouseUp(Events.MouseUserInput input);
        }

        public class UserInputProvider : List<IUserInputListener>, IUserInputListener
        {
            private bool mEnable = true;

            public void Enable(bool mode)
            {
                mEnable = mode;
            }

            public virtual void OnKeyDown(UserInput input)
            {
                if (mEnable)
                {
                    foreach (IUserInputListener it in this)
                    {
                        it.OnKeyDown(input);
                    }
                }
            }

            public virtual void OnKeyUp(UserInput input)
            {
                if (mEnable)
                {
                    foreach (IUserInputListener it in this)
                    {
                        it.OnKeyUp(input);
                    }
                }
            }

            public virtual void OnMouseMove(Events.MouseUserInput input)
            {
                if (mEnable)
                {
                    foreach (IUserInputListener it in this)
                    {
                        it.OnMouseMove(input);
                    }
                }
            }

            public virtual void OnMouseDown(Events.MouseUserInput input)
            {
                if (mEnable)
                {
                    foreach (IUserInputListener it in this)
                    {
                        it.OnMouseDown(input);
                    }
                }
            }

            public virtual void OnMouseUp(Events.MouseUserInput input)
            {
                if (mEnable)
                {
                    foreach (IUserInputListener it in this)
                    {
                        it.OnMouseUp(input);
                    }
                }
            }
        }
    }
}